﻿using JuCheap.Core.Infrastructure.Extentions;
using JuCheap.Core.Infrastructure.Utilities;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using static JuCheap.Core.Infrastructure.Utilities.Constants;

namespace JuCheap.Core.Infrastructure.Web
{
    /// <summary>
    /// 当前登陆用户信息服务(仅限web应用程序使用;其它应用程序(如Job,Winform等)请自行实现ICurrentUserService)
    /// </summary>
    public class CurrentUserService : ICurrentUserService
    {
        private readonly IHttpContextAccessor _context;
        private readonly IEnumerable<Claim> _claims;

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="context"></param>
        public CurrentUserService(IHttpContextAccessor context)
        {
            _context = context;
            _claims = _context.HttpContext?.User?.Claims;
        }

        /// <summary>
        /// 获取当前登陆公司的Id
        /// </summary>
        /// <returns></returns>
        public string GetCompanyId()
        {
            var companyId = GetClaimValue(ClaimType.CompanyId);
            return companyId;
        }

        /// <summary>
        /// 获取当前登陆用户的Id
        /// </summary>
        /// <returns></returns>
        public string GetUserId()
        {
            var userId = GetClaimValue(ClaimType.UserId);
            return userId;
        }

        /// <summary>
        /// 是否是管理员
        /// </summary>
        /// <returns></returns>
        public bool IsAdmin()
        {
            return bool.Parse(GetClaimValue(ClaimType.IsAdmin, bool.FalseString.ToLower()));
        }

        /// <summary>
        /// 是否是系统公司
        /// </summary>
        /// <returns></returns>
        public bool IsSystemCompany()
        {
            return bool.Parse(GetClaimValue(ClaimType.IsSystemCompany, bool.FalseString.ToLower()));
        }

        /// <summary>
        /// 获取当前登陆用户信息
        /// </summary>
        /// <returns></returns>
        public CurrentUserDto GetCurrentUser()
        {
            var userId = GetClaimValue(ClaimType.UserId);
            var companyId = GetClaimValue(ClaimType.CompanyId);
            var loginAccount = GetClaimValue(ClaimType.Name);
            var userName = GetClaimValue(ClaimType.UserName);
            var companyName = GetClaimValue(ClaimType.CompanyName);
            var isAdmin = bool.Parse(GetClaimValue(ClaimType.IsAdmin, bool.FalseString.ToLower()));
            var isSystemCompany = bool.Parse(GetClaimValue(ClaimType.IsSystemCompany, bool.FalseString.ToLower()));
            return new CurrentUserDto
            {
                UserId = userId,
                CompanyId = companyId,
                Name = userName,
                CompanyName = companyName,
                LoginName = loginAccount,
                IsAdmin = isAdmin,
                IsSystemCompany = isSystemCompany
            };
        }

        /// <summary>
        /// 获取User认证信息中的Claim信息
        /// </summary>
        /// <param name="type"></param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        private string GetClaimValue(string type, string defaultValue = "")
        {
            if (!_claims.AnyOne())
            {
                return defaultValue;
            }
            var value = _claims.FirstOrDefault(x => x.Type == type)?.Value;
            if (value.IsBlank())
            {
                return defaultValue;
            }
            return value;
        }
    }
}
